package com_.dxy_test_.file_copy_;

import java.io.File;

/**
 * @Description 重命名文件
 * @Author dangxianyue
 * @Date 2022/10/28 17:52
 */
public class RenameFileTest {

    public static void main(String[] args) {
        changeFileName("C:\\Users\\Alex\\Desktop\\demo");
    }

    public static void changeFileName(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        changeFileName(file2.getAbsolutePath());
                    } else {
                        String filePath = file2.getAbsolutePath();
                        if (filePath.endsWith(".javatxt")) {
                            String newFileName = filePath.replace(".javatxt", ".java");
                            File oriFile = new File(filePath);
                            if (oriFile.renameTo(new File(newFileName))) {
                                System.out.println("文件【" + filePath + "】改名成功！");
                            }

                        }

                    }
                }
            }
        } else {
            System.out.println("该路径不存在");
        }

    }
}
